package controller;

import com.jfinal.core.Controller;
import javafx.scene.control.Alert;
import model.Userinfo;

public class IndexController extends Controller {
    public void index(){
        renderHtml("Hello world !");
    }
    public void hello(){
        String username = getPara("username");
        int age = getParaToInt("age");
        renderHtml("<h1>你好,"+username+"   你今年"+age+"岁</h1>");
    }

    public void main(){
        String username = getSessionAttr("username");
        renderHtml("主页");

    }

    public void manager(){
        renderHtml("管理页面");
    }

    public void login(){
        renderFreeMarker("login.ftl");
    }

    public void doLogin(){
        String user = getPara("user");
        String password = getPara("password");
        if(user.equals("admin")&&password.equals("123456")){
            redirect("/main");
            setSessionAttr("user",user);
        }else {
            setAttr("errmsg","用户名或密码错误");
            renderFreeMarker("login.ftl");
        }
    }

    public void register(){
        renderFreeMarker("register.ftl");
    }
    public void doRegister(){
        String user = getPara("user");
        String password = getPara("password");
        Integer sex = getParaToInt("sex");
        String hometown = getPara("hometown");

        Userinfo us = new Userinfo();
        us.setUser(user);
        us.setPassword(password);
        us.setSex(sex);
        us.setHometown(hometown);

        us.save();
        renderFreeMarker("login.ftl");
    }
}
